package net.zjitc.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import net.zjitc.common.BaseContext;
import net.zjitc.common.R;
import net.zjitc.dto.OrderDto;
import net.zjitc.entiy.OrderDetail;
import net.zjitc.entiy.Orders;
import net.zjitc.entiy.ShoppingCart;
import net.zjitc.service.OrderDetailService;
import net.zjitc.service.OrderService;
import net.zjitc.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private OrderDetailService orderDetailService;
    //用户下单
    //请求 URL: http://localhost:8080/order/submit
    //请求方法: POST
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        log.info("订单数据：{}",orders);
        orderService.submit(orders);
        return R.success("下单成功");
    }
    //查询订单
    //请求 URL: http://localhost:8080/order/userPage?page=1&pageSize=5
    //请求方法: GET
    @GetMapping("/userPage")
    public R<Page<OrderDto>> page(Integer page,Integer pageSize){
        log.info("page,pageSize"+page+","+pageSize);
        Page<OrderDto> orderDtoPage = orderService.pageWithDetail(page, pageSize);
        return R.success(orderDtoPage);
    }

    //后端查询订单
    //请求 URL: http://localhost:8080/order/page?page=1&pageSize=10
    //请求方法: GET
    @GetMapping("/page")
    public R<Page<Orders>> page(int page, int pageSize, String number, String beginTime, String endTime){
        log.info("page = {},pageSize = {},number= {},beginTime={},endTime={}",page,pageSize,number,beginTime,endTime);
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(number!=null,Orders::getNumber,number);
        wrapper.ge(beginTime!=null,Orders::getOrderTime,beginTime);
        wrapper.le(endTime!=null,Orders::getOrderTime,endTime);
        wrapper.orderByDesc(Orders::getCheckoutTime);
        Page<Orders> ordersPage = new Page<>(page,pageSize);
        return R.success(orderService.page(ordersPage,wrapper));
    }
    //订单派送
    //请求 URL: http://localhost:8080/order
    //请求方法: PUT
    @PutMapping
    public R<String> put(@RequestBody Orders orders){
        orderService.updateById(orders);
        return R.success("订单派送成功");
    }
    //请求 URL: http://localhost:8080/order/again
    //请求方法: POST
    //再来一单
    @PostMapping("/again")
    public R<String> againSubmit(@RequestBody Map<String,String> map){
        String ids = map.get("id");

        long id = Long.parseLong(ids);

        LambdaQueryWrapper<OrderDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrderDetail::getOrderId,id);
        List<OrderDetail> orderDetailList = orderDetailService.list(queryWrapper);

        orderDetailService.removeById(BaseContext.getCurrentId());

        Long userId = BaseContext.getCurrentId();
        List<ShoppingCart> shoppingCartList = orderDetailList.stream().map((item) -> {
            ShoppingCart shoppingCart = new ShoppingCart();
            shoppingCart.setUserId(userId);
            shoppingCart.setImage(item.getImage());
            Long dishId = item.getDishId();
            Long setmealId = item.getSetmealId();
            if (dishId != null) {
                shoppingCart.setDishId(dishId);
            } else {
                //添加到购物车的是套餐
                shoppingCart.setSetmealId(setmealId);
            }
            shoppingCart.setName(item.getName());
            shoppingCart.setDishFlavor(item.getDishFlavor());
            shoppingCart.setNumber(item.getNumber());
            shoppingCart.setAmount(item.getAmount());
            shoppingCart.setCreateTime(LocalDateTime.now());
            return shoppingCart;
        }).collect(Collectors.toList());
        shoppingCartService.saveBatch(shoppingCartList);

        return R.success("操作成功");
    }

}
